<?php

namespace app\services\Index;

use app\model\advert\Advert;
use app\model\user\UserNotice;
use app\Request;
use app\services\activity\seckill\StoreSeckillServices;
use app\services\product\product\StoreProductLabelServices;
use app\services\product\product\StoreProductServices;
use Carbon\Carbon;

class IndexService
{
    public function indexList($user)
    {
        
        // 获取banner
        $banner = Advert::where('pid', 1)->where('status', 1)->where(function ($query) {
            $query->where('time_type', 0)->whereOr(function ($query) {
               $time = Carbon::now();
               $query->where('time_type', 1)->where('start_time', '<=', $time)->where('end_time', '>', $time);
            });
        })->field('id,title,url,image_input')->select();

        // 获取通知
        $notices = UserNotice::when(!$user, function ($query) {
            $query->where('type', 1);
        })->when($user, function ($query) use ($user) {
            $query->whereRaw('JSON_CONTAINS(uid, ?)', [json_encode([$user->uid])]);
        })->order('id', 'desc')
            ->limit(10)->field('content')->select();

        // 获取秒杀首页展示
        $seckillService = app()->make(StoreSeckillServices::class);
        $seckill = $seckillService->getHomeShow();

        // 获取 分享赚佣金
        $productService = app()->make(StoreProductServices::class);
        $agent = $productService->getHomeSeckillProductList($user);

        // 获取 商品标签
        $productLabelService = app()->make(StoreProductLabelServices::class);
        $productLabels = $productLabelService->getHomeShow();

        $products = $productLabelService->getHomeProductList(0);

        // 获取商品数据
        return compact('banner', 'notices', 'seckill', 'agent', 'productLabels', 'products');

    }

    public function getHomeProductList($label_id, $sort_by = 0, $sort_value = 0)
    {
        $productLabelService = app()->make(StoreProductLabelServices::class);
        return $productLabelService->getHomeProductList($label_id, $sort_by, $sort_value);
    }
}